<template>
	<view class="popup-modal">
		<view class="popup-mask" @click="close"></view>
		<view class="popup-content">
			<view class="popup-title">
				<text>{{title}}</text>
				<text v-if="multiple">(可多选)</text>
			</view>
			<view class="popup-main" v-if="multipleArr">
				<view v-for="(groupItem,groupIndex) in options" :key="groupIndex" class="group">
					<view class="groupItem">{{groupItem.value}}</view>
					<view v-for="(item,index) in groupItem.children" :key="index" class="option-item"
						:class="isSelect(item) ? 'option-item-select':''" @click="select(item)">
						{{item.label}}
					</view>
				</view>
			</view>
			<view class="popup-main" v-else>
				<view v-for="(item,index) in options" :key="index" class="option-item"
					:class="isSelect(item) ? 'option-item-select':''" @click="select(item)">
					{{item.label}}
				</view>
			</view>
			<view class="popup-button">
				<button type="primary" plain @click="clear">不限</button>
				<button type="primary" plain @click="confirm">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'custom-modal',
		props: {
			isShowPopup: {
				type: Boolean,
				default: false,
			},
			title: {
				type: String,
				default: '',
			},
			options: {
				type: Array,
				default: () => ([])
			},
			selOptions: {
				type: Array,
				default: () => ([])
			},
			multiple: {
				type: Boolean,
				default: false,
			},
			multipleArr: {
				type: Boolean,
				default: false,
			},
		},
		data() {
			return {
				showCustomPopup: false,
				activeOptions: [],
			};
		},
		methods: {
			isSelect(item) {
				return this.activeOptions.some(li => li.id === item.id)
			},
			select(item) {
				if (this.multiple) {
					const index = this.activeOptions.findIndex(li => li.id === item.id);
					if (index > -1) {
						this.activeOptions.splice(index, 1);
					} else {
						this.activeOptions.push(item);
					}
				} else {
					this.activeOptions = [item];
				}
			},
			clear() {
				this.activeOptions = [];
			},
			close() {
				this.$emit('close');
			},
			confirm() {
				this.$emit('confirm', this.activeOptions)
			}
		},
		mounted() {
			this.showCustomPopup = this.isShowPopup;
			this.activeOptions = this.selOptions;
		}
	};
</script>

<style lang="scss">
	.popup-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 99;
	}

	.popup-mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.popup-content {
		background-color: #fff;
		padding: 30rpx 0;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		width: 100%;
		box-sizing: border-box;
		height: 60%;
		position: fixed;
		left: 0;
		bottom: 120rpx;
		display: flex;
		flex-direction: column;
	}

	.popup-title {
		padding: 0 30rpx;
		font-size: 28rpx;
		text-align: left;
		height: 80rpx;
		line-height: 80rpx;
	}

	.popup-main {
		flex: 1;
		overflow: auto;
		padding: 0 30rpx;
		.groupItem{
			font-size: 28rpx;
			text-align: left;
			height: 70rpx;
			line-height: 70rpx;
		}

		.option-item {
			width: calc((100% - 90rpx)/ 3);
			height: 80rpx;
			border-radius: 10rpx;
			background: #F5F5F5;
			color: #333333;
			font-size: 26rpx;
			display: inline-flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 16rpx;
			margin-right: 45rpx;
		}

		.option-item:nth-child(3n) {
			margin-right: 0;
		}

		.option-item-select {
			background: #EDF4FF;
			color: #2979FF;
		}
	}

	.popup-button {
		display: flex;
		padding: 0 10px;
		button {
			flex: 1;
		}

		button:first-child {
			margin-right: 50rpx;
		}
	}
</style>